#include "../include/sdl_world.h"


SDL_World::SDL_World(int windowWidth, int windowHeight, int FPS)
{
    //Initiate SDL
    SDL_Init( SDL_INIT_VIDEO );

    //Setup the screen surface
    screen = SDL_SetVideoMode( windowWidth, windowHeight, 0,
                               SDL_HWSURFACE | SDL_DOUBLEBUF );

    //Setup the framerate manager and initiate it with a framerate of 60
    frameManagement = new FPSmanager();
    SDL_initFramerate (frameManagement);
    SDL_setFramerate (frameManagement, FPS);

    //Setup the screen objects
    sceneImages = new std::vector<Drawable*>();

}

SDL_World::~SDL_World()
{
    SDL_Quit();
}


void SDL_World::AddToScene(Drawable* object)
{
    sceneImages->push_back(object);
}

//Responsible for drawing everything
void SDL_World::Draw()
{
    //Blank out the screen
    SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));

    //Go through each of the scene images and draw them
    for(int i = 0; i < (int) sceneImages->size(); i++)
    {
        //Make sure the value isn't NULL
        if( !(sceneImages->at(i) == NULL ) )
        {
            //Draw the object
            sceneImages->at(i)->Draw(screen);
        }
    }

    //Flip
    SDL_Flip(screen);

    //Pause
    SDL_framerateDelay(frameManagement);
}
